package helpServlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.helpSession.HelpRequestsessionRemote;
import entities.HelpRequest;

/**
 * Servlet implementation class showHelps
 */
public class showHelpsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		drawPage(request, response);
	}

	public void drawPage(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id = (Integer) request.getSession().getAttribute("id");
		ArrayList<HelpRequest> list;
		Context jndiCtx;

		try {
			jndiCtx = new InitialContext();
			HelpRequestsessionRemote hrs = (HelpRequestsessionRemote) jndiCtx
					.lookup("Progetto/HelpRequestsession/remote");

			list = (ArrayList<HelpRequest>) hrs.sentRequests(id);
			request.getSession().setAttribute("sentRequest", list);

			list = (ArrayList<HelpRequest>) hrs.receivedRequests(id);
			request.getSession().setAttribute("receivedHelpRequest", list);

			list = (ArrayList<HelpRequest>) hrs.receivedHelps(id);
			request.getSession().setAttribute("sentHelp", list);

			list = (ArrayList<HelpRequest>) hrs.givenHelps(id);
			request.getSession().setAttribute("receivedHelp", list);
			
			list = (ArrayList<HelpRequest>) hrs.refusedHelps(id);
			request.getSession().setAttribute("refusedHelps", list);

		} catch (NamingException e) {
			request.getSession().setAttribute("done", "error");
		}
		response.sendRedirect("/ProgettoWeb/RegisteredUser/ShowHelps.jsp");
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		drawPage(request, response);
	}

}
